class ResourcesController < ApplicationController
  
  before_filter :login_required, :only => [:new, :edit, :create, :update, :destroy]
  before_filter :set_current_menu_and_title

  def index
    @resources = Resource.paginate(:per_page => 10, :page => params[:page], :order => 'created_at desc')
    @rss_resources = Resource.find(:all, :limit => 10, :order => 'created_at desc')
  end
  
  def show
    @resource = Resource.find(params[:id])
    @title = @resource.title
    @keywords = @resource.tag_list.join(' ')
    @description = @resource.description[0,100]
  end

  def new
    @resource = Resource.new
  end

  def edit
    @resource = Resource.find(params[:id])
    if @resource.user.id != current_user.id
      redirect_to "/not_found"
    end
  end

  def create
    @resource = Resource.new(params[:resource])
    @resource.user_id = current_user.id

    if @resource.save
      flash[:notice] = 'Resource was successfully created.'
      redirect_to(@resource)
    else
      render :action => "new"
    end
  end

  def update
    @resource = Resource.find(params[:id])
    @resource.user_id = current_user.id

    if @resource.update_attributes(params[:resource])
      flash[:notice] = 'Resource was successfully updated.'
      redirect_to(@resource)
    else
      render :action => "edit"
    end
  end

  def destroy
    @resource = Resource.find(params[:id])
    @resource.destroy

    redirect_to(resources_url)
  end
  
  private
    def set_current_menu_and_title
      @current_menu = :resource
      @title = "资源"
    end
  
end
